<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改工作量上报</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/Sys/css/layui.css">
    <link rel="stylesheet" href="/Sys/css/sign.css">
    <link rel="stylesheet" href="/Sys/css/font.css">
    <link rel="stylesheet" href="/Sys/css/xadmin.css">
    <script type="text/javascript" src="/Sys/js/jquery-3.3.1.min.js"></script>
    <script src="/Sys/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/Sys/js/xadmin.js"></script>
</head>

<body>
    <div class="layui-container" style="padding: 20px;">
        <form class="layui-form" action="">
            <input type="hidden" name="id">
            
            <div class="layui-form-item">
                <label class="layui-form-label">学年</label>
                <div class="layui-input-block">
                    <input type="text" name="year" required lay-verify="required" placeholder="请输入学年"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">学期</label>
                <div class="layui-input-block">
                    <select name="semester" lay-verify="required">
                        <option value="">请选择学期</option>
                        <option value="上学期">上学期</option>
                        <option value="下学期">下学期</option>
                    </select>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">月份</label>
                <div class="layui-input-block">
                    <input type="number" name="month" required lay-verify="required|month" placeholder="请输入月份(1-12)"
                        autocomplete="off" class="layui-input" min="1" max="12">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">科室名称</label>
                <div class="layui-input-block">
                    <input type="text" name="departmentName" required lay-verify="required" placeholder="请输入科室名称"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">负责老师</label>
                <div class="layui-input-block">
                    <input type="text" name="teacher" required lay-verify="required" placeholder="请输入负责老师"
                        autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">工作时长</label>
                <div class="layui-input-block">
                    <input type="number" name="workingHours" required lay-verify="required|workingHours" placeholder="请输入该月工作时长(不超过50)"
                        autocomplete="off" class="layui-input" min="1" max="50">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">时间段（工作日）</label>
                <div class="layui-input-block">
                    <select name="timeSlot" lay-verify="required">
                        <option value="">请选择时间段</option>
                        <option value="08:00-10:00">08:00-10:00</option>
                        <option value="10:00-12:00">10:00-12:00</option>
                        <option value="14:00-16:00">14:00-16:00</option>
                        <option value="16:00-18:00">16:00-18:00</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        layui.use(['form', 'layer'], function () {
            var form = layui.form,
                layer = layui.layer;

            // 自定义验证规则
            form.verify({
                month: function(value) {
                    if(value < 1 || value > 12) {
                        return '月份必须在1-12之间';
                    }
                },
                workingHours: function(value) {
                    if(value < 1) {
                        return '工作时长必须大于0';
                    }
                    if(value > 50) {
                        return '工作时长不能超过50小时';
                    }
                }
            });

            // 获取URL中的id参数
            var id = getUrlParam('id');
            if (id) {
                // 加载工作量上报数据
                $.ajax({
                    url: '/Sys/getWorkPush',
                    type: 'GET',
                    data: { id: id },
                    success: function (res) {
                        if (res.code === 0) {
                            var workPush = res.data;
                            // 填充表单
                            $('input[name="id"]').val(workPush.id);
                            $('input[name="year"]').val(workPush.year);
                            $('select[name="semester"]').val(workPush.semester);
                            $('input[name="month"]').val(workPush.month);
                            $('input[name="departmentName"]').val(workPush.departmentName);
                            $('input[name="teacher"]').val(workPush.teacher);
                            $('input[name="workingHours"]').val(workPush.workingHours);
                            $('select[name="timeSlot"]').val(workPush.timeSlot);
                            form.render();
                        } else {
                            layer.msg('加载数据失败', {icon: 2});
                        }
                    }
                });
            }

            //监听提交
            form.on('submit(formSubmit)', function (data) {
                data.field.userId = '[[${session.id}]]';
                $.ajax({
                    url: '/Sys/updateWorkPush',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(data.field),
                    success: function (res) {
                        if (res === "success") {
                            layer.msg('修改成功', {
                                icon: 1,
                                time: 2000
                            }, function(){
                                // 关闭当前frame
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                // 刷新父页面
                                parent.location.reload();
                            });
                        } else if (res === "approved") {
                            layer.msg('该记录已审批通过，不能修改', {
                                icon: 2,
                                time: 2000
                            });
                        } else if (res === "invalid_hours") {
                            layer.msg('工作时长必须在1-50小时之间', {
                                icon: 2,
                                time: 2000
                            });
                        } else if (res === "duplicate_month") {
                            layer.msg('当月已有工作量上报记录', {
                                icon: 2,
                                time: 2000
                            });
                        } else {
                            layer.msg('修改失败，请重试', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    },
                    error: function () {
                        layer.msg('服务器错误，请联系管理员', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
                return false;
            });
        });

        // 获取URL参数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURI(r[2]); return null;
        }
    </script>
</body>
</html>